package com.cy.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;

@Component
@Aspect
@Slf4j
public class LogAspect {
    @Pointcut("@annotation(com.cy.annatation.RequredLog)")
    public void doLog() {
    }

    @Around("doLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long t1 = System.currentTimeMillis();
        log.info("start {}", t1);
        try {
            Object result = joinPoint.proceed();
            long t2 = System.currentTimeMillis();
            log.info("end {} ", t2);
            log.info("total {}", t2 - t1);
            return result;
        } catch (Throwable throwable) {
            log.error("error{} ", throwable);
            throw throwable;
        }

    }
}
